
import type { RouteObject } from 'react-router-dom';
import { Navigate } from 'react-router-dom';
import About from '../../wang/about';
import LoginPage from '../../yang/LoginPage';
import RegisterPage from '../../yang/RegisterPage';
import Appear from '../../wang/appear';
import VideoFeed from '../../liu/VideoFeed';
import QQLoginTest from '../../yang/QQLoginTest';

// 鉴权函数：检查localStorage中是否存在token
const isAuthenticated = () => {
  return localStorage.getItem('token') !== null;
};

// 受保护路由组件
const ProtectedRoute = ({ element }: { element: React.ReactNode }) => {
  return isAuthenticated() ? element : <Navigate to="/login" replace />;
};

const routes: RouteObject[] = [
  { path: '/', element: <Navigate to='/about' replace /> },
  { path: '/about', element: <ProtectedRoute element={<About />} /> },
  { path: '/login', element: <LoginPage /> },
  { path: '/register', element: <RegisterPage /> },
  { path: '/appear', element: <ProtectedRoute element={<Appear />} /> },
  { path: '/video-feed', element: <ProtectedRoute element={<VideoFeed />} /> },
  { path: '/qq-test', element: <QQLoginTest /> }
];

export default routes;